<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\Note;
use app\api\model\dao\NotePraise;
use app\api\model\dao\NoteReply;
use app\api\model\dao\NoteReplyPraise;
use app\api\model\dao\NoteReplyReply;
use app\api\model\dao\UserAccount;
use app\api\model\data\note\NoteData;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class GetUnreadPraiseList extends PageBase
{

    private $objNote;
    private $objNoteReply;
    private $objNoteReplyReply;
    private $objNoteReplyPraise;
    private $objUserAccount;
    private $objNoteData;
    private $objNotePraise;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNoteReply = new NoteReply();
        $this->objUserAccount = new UserAccount();
        $this->objNoteReplyReply = new NoteReplyReply();
        $this->objNoteReplyPraise = new NoteReplyPraise();
        $this->objNoteData = new NoteData();
        $this->objNotePraise = new NotePraise();
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     * @throws Exception
     */
    public function excute($param)
    {
        $this->checkToken($param);

        $this->user_id = !empty($this->user_id) ? $this->user_id : 0;

        $this->data['list'] = [];

        if (empty($this->user_id)) {
            return $this->getRes();
        }

        $userids = [];
        $userid2Time = [];
        $user_account = [];

        $conds = [
            'note_userid' => ['=', $this->user_id],
            //'status' => ['=', 0],
            'is_delete' => ['=', 0],
        ];
        $note_praise_ret = $this->objNotePraise->getNotePraise($conds, ['userid', 'create_time']);
        if (empty($note_praise_ret)) {
            $note_praise_ret = [];
        }
        foreach ($note_praise_ret as $val) {
            $userids[] = $val['userid'];
            if (empty($userid2Time[$val['userid']]) || $userid2Time[$val['userid']] < $val['create_time']) {
                $userid2Time[$val['userid']] = $val['create_time'];
            }
        }

        $conds = [
            'recv_userid' => ['=', $this->user_id],
            //'status' => ['=', 0],
        ];
        $note_reply_praise_ret = $this->objNoteReplyPraise->getList($conds, ['userid', 'create_time']);
        if (empty($note_reply_praise_ret)) {
            $note_reply_praise_ret = [];
        }
        foreach ($note_reply_praise_ret as $val) {
            $userids[] = $val['userid'];
            if (empty($userid2Time[$val['userid']]) || $userid2Time[$val['userid']] < $val['create_time']) {
                $userid2Time[$val['userid']] = $val['create_time'];
            }
        }

        if (!empty($userids)) {
            $user_account_ret = $this->objUserAccount->getUserAccountByUserids($userids);
            if (empty($user_account_ret)) {
                $user_account_ret = [];
            }
            foreach ($user_account_ret as $user) {
                $user_account[$user['id']] = $user;
            }
        }

        foreach ($user_account as $user) {
            $this->data['list'][] = [
                'userid' => $user['id'],
                'nickname' => $user['nick_name'],
                'photo' => $user['avatar'],
                'time' => !empty($userid2Time[$user['id']]) ? $this->objNoteData->caleDate($userid2Time[$user['id']]) : '',
            ];
        }

        // 标记已读
        $this->objNotePraise->read($this->user_id);
        $this->objNoteReplyPraise->read($this->user_id);

        return $this->getRes();
    }
}
